package com.csnf.hymall.dao.imp;

import com.csnf.hymall.dao.BaseMapper;
import com.csnf.hymall.util.JdbcUtil;
import com.csnf.hymall.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseMapperImp implements BaseMapper {
    protected Logger logger = LogManager.getLogger(BaseMapperImp.class);// 日志

    @Override
    public Integer getTotalCount(String sql, Object... obj) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn, sql, obj);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        }
        JdbcUtil.close(conn, ps);
        return null;
    }

    @Override
    public Integer add(String sql, Object... params) throws SQLException {
        return update(sql, params);
    }

    @Override
    public Integer update(String sql, Object... params) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn, sql, params);
        int count = ps.executeUpdate();
        JdbcUtil.close(conn, ps);
        return count;
    }

    @Override
    public Integer delete(String sql, Object... params) throws SQLException {
        return update(sql, params);
    }

    @Override
    public <T> T get(String sql, Class<T> tClass, Object... params) throws Exception {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn, sql, params);
        ResultSet rs = ps.executeQuery();
        T t = null;
        ResultSetMetaData md = rs.getMetaData();// 获取列的元数据
        while (rs.next()) {
            t = JdbcUtil.invokeObject(rs, tClass);
        }
        JdbcUtil.close(conn, ps, rs);
        return t;
    }


    @Override
    public <T> List<T> getList(String sql, Class<T> tClass, Object... params) throws Exception {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn, sql, params);
        ResultSet rs = ps.executeQuery();
        ArrayList<T> results = new ArrayList<T>();
        while (rs.next()) {
            results.add(JdbcUtil.invokeObject(rs, tClass));
        }
        JdbcUtil.close(conn, ps, rs);
        return results;
    }

    @Override
    public Map<String, Object> queryMap(String sql, Object... params) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn, sql, params);
        ResultSet rs = ps.executeQuery();
        ResultSetMetaData md = rs.getMetaData();
        int count = md.getColumnCount();
        HashMap<String, Object> map = new HashMap<String, Object>();
        while (rs.next()) {
            for (int i = 1; i <= count; i++) {
                String columnName = md.getColumnName(i);
                String labelName = md.getColumnLabel(i);
                columnName = columnName.equals(labelName) ? columnName : labelName;
                map.put(columnName, rs.getObject(columnName));
            }
        }
        JdbcUtil.close(conn, ps, rs);
        return map;
    }

    @Override
    public List<Map<String, Object>> queryList(String sql, Object... params) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement ps = JdbcUtil.getPreparedStatement(conn, sql, params);
        ResultSet rs = ps.executeQuery();
        ResultSetMetaData md = rs.getMetaData();
        int count = md.getColumnCount();
        ArrayList<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
        while (rs.next()) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            for (int i = 1; i <= count; i++) {
                String columnName = md.getColumnName(i);
                String labelName = md.getColumnLabel(i);
                columnName = columnName.equals(labelName) ? columnName : labelName;
                map.put(columnName, rs.getObject(columnName));
            }
            results.add(map);
        }
        JdbcUtil.close(conn, ps, rs);
        return results;
    }


}
